<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book django项目 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Session | django项目</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C04-ClassViewAndMiddleware/index.html" />
    
    
    <link rel="prev" href="../C03-RequestAndResponse/Cookie.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="4.4"
        data-chapter-title="Session"
        data-filepath="C03-RequestAndResponse/Session.md"
        data-basepath=".."
        data-revision="Wed Nov 14 2018 07:27:32 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python Web 框架要点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutDjango.html">
            
                
                    <a href="../AboutDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/StartProject.html">
            
                
                    <a href="../C01-CreateProject/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/StartApp.html">
            
                
                    <a href="../C01-CreateProject/StartApp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        创建子应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/CreateView.html">
            
                
                    <a href="../C01-CreateProject/CreateView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        创建视图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-SettingsStaticAndURL/index.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        配置、静态文件与路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-SettingsStaticAndURL/Settings.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/Settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-SettingsStaticAndURL/StaticFiles.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/StaticFiles.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C02-SettingsStaticAndURL/URL.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/URL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        路由说明
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/Request.html">
            
                
                    <a href="../C03-RequestAndResponse/Request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        请求Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/Response.html">
            
                
                    <a href="../C03-RequestAndResponse/Response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        响应Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/Cookie.html">
            
                
                    <a href="../C03-RequestAndResponse/Cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.4" data-path="C03-RequestAndResponse/Session.html">
            
                
                    <a href="../C03-RequestAndResponse/Session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-ClassViewAndMiddleware/index.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        类视图与中间件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-ClassViewAndMiddleware/ClassView.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/ClassView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-ClassViewAndMiddleware/Middleware.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Template/index.html">
            
                
                    <a href="../C05-Template/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Template/template.html">
            
                
                    <a href="../C05-Template/template.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        模板使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C06-Database/index.html">
            
                
                    <a href="../C06-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C06-Database/Config.html">
            
                
                    <a href="../C06-Database/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="C06-Database/Model.html">
            
                
                    <a href="../C06-Database/Model.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="C06-Database/Tools.html">
            
                
                    <a href="../C06-Database/Tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        演示工具使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="C06-Database/CURD.html">
            
                
                    <a href="../C06-Database/CURD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        数据库操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="C06-Database/QuerySet.html">
            
                
                    <a href="../C06-Database/QuerySet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        查询集QuerySet
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C07-Admin/index.html">
            
                
                    <a href="../C07-Admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        Admin站点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C07-Admin/Config.html">
            
                
                    <a href="../C07-Admin/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        使用Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="C07-Admin/ModifyListDisplay.html">
            
                
                    <a href="../C07-Admin/ModifyListDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        调整列表页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="C07-Admin/ModifyEditDisplay.html">
            
                
                    <a href="../C07-Admin/ModifyEditDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        调整编辑页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="C07-Admin/ModifySite.html">
            
                
                    <a href="../C07-Admin/ModifySite.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        调整站点信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="C07-Admin/UploadImage.html">
            
                
                    <a href="../C07-Admin/UploadImage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >django项目</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="session">Session</h1>
<h2 id="1-&#x542F;&#x7528;session">1 &#x542F;&#x7528;Session</h2>
<p><strong>Django&#x9879;&#x76EE;&#x9ED8;&#x8BA4;&#x542F;&#x7528;Session&#x3002;</strong></p>
<p>&#x53EF;&#x4EE5;&#x5728;settings.py&#x6587;&#x4EF6;&#x4E2D;&#x67E5;&#x770B;&#xFF0C;&#x5982;&#x56FE;&#x6240;&#x793A;</p>
<p><img src="../images/session_middleware.png" alt="session&#x4E2D;&#x95F4;&#x4EF6;"></p>
<p>&#x5982;&#x9700;&#x7981;&#x7528;session&#xFF0C;&#x5C06;&#x4E0A;&#x56FE;&#x4E2D;&#x7684;session&#x4E2D;&#x95F4;&#x4EF6;&#x6CE8;&#x91CA;&#x6389;&#x5373;&#x53EF;&#x3002;</p>
<h2 id="2-&#x5B58;&#x50A8;&#x65B9;&#x5F0F;">2 &#x5B58;&#x50A8;&#x65B9;&#x5F0F;</h2>
<p>&#x5728;settings.py&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;session&#x6570;&#x636E;&#x7684;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x4FDD;&#x5B58;&#x5728;&#x6570;&#x636E;&#x5E93;&#x3001;&#x672C;&#x5730;&#x7F13;&#x5B58;&#x7B49;&#x3002;</p>
<h3 id="21-&#x6570;&#x636E;&#x5E93;">2.1 &#x6570;&#x636E;&#x5E93;</h3>
<p>&#x5B58;&#x50A8;&#x5728;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#xFF0C;&#x5982;&#x4E0B;&#x8BBE;&#x7F6E;&#x53EF;&#x4EE5;&#x5199;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4E0D;&#x5199;&#xFF0C;<strong>&#x8FD9;&#x662F;&#x9ED8;&#x8BA4;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;</strong>&#x3002;</p>
<pre><code class="lang-python">SESSION_ENGINE=<span class="hljs-string">&apos;django.contrib.sessions.backends.db&apos;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x5B58;&#x50A8;&#x5728;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x9879;INSTALLED_APPS&#x4E2D;&#x5B89;&#x88C5;Session&#x5E94;&#x7528;&#x3002;</p>
<p><img src="../images/session_app.png" alt="session_app"></p>
<p>&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x8868;&#x5982;&#x56FE;&#x6240;&#x793A;</p>
<p><img src="../images/session_database.png" alt="session&#x6570;&#x636E;&#x5E93;"></p>
<p>&#x8868;&#x7ED3;&#x6784;&#x5982;&#x4E0B;</p>
<p><img src="../images/session_table.png" alt="session&#x8868;&#x7ED3;&#x6784;"></p>
<p>&#x7531;&#x8868;&#x7ED3;&#x6784;&#x53EF;&#x77E5;&#xFF0C;&#x64CD;&#x4F5C;Session&#x5305;&#x62EC;&#x4E09;&#x4E2A;&#x6570;&#x636E;&#xFF1A;&#x952E;&#xFF0C;&#x503C;&#xFF0C;&#x8FC7;&#x671F;&#x65F6;&#x95F4;&#x3002;</p>
<h3 id="22-&#x672C;&#x5730;&#x7F13;&#x5B58;">2.2 &#x672C;&#x5730;&#x7F13;&#x5B58;</h3>
<p>&#x5B58;&#x50A8;&#x5728;&#x672C;&#x673A;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x4E22;&#x5931;&#x5219;&#x4E0D;&#x80FD;&#x627E;&#x56DE;&#xFF0C;&#x6BD4;&#x6570;&#x636E;&#x5E93;&#x7684;&#x65B9;&#x5F0F;&#x8BFB;&#x5199;&#x66F4;&#x5FEB;&#x3002;</p>
<pre><code class="lang-python">SESSION_ENGINE=<span class="hljs-string">&apos;django.contrib.sessions.backends.cache&apos;</span>
</code></pre>
<h3 id="23-&#x6DF7;&#x5408;&#x5B58;&#x50A8;">2.3 &#x6DF7;&#x5408;&#x5B58;&#x50A8;</h3>
<p>&#x4F18;&#x5148;&#x4ECE;&#x672C;&#x673A;&#x5185;&#x5B58;&#x4E2D;&#x5B58;&#x53D6;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5219;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x5B58;&#x53D6;&#x3002;</p>
<pre><code class="lang-python">SESSION_ENGINE=<span class="hljs-string">&apos;django.contrib.sessions.backends.cached_db&apos;</span>
</code></pre>
<h3 id="24-redis">2.4 Redis</h3>
<p>&#x5728;redis&#x4E2D;&#x4FDD;&#x5B58;session&#xFF0C;&#x9700;&#x8981;&#x5F15;&#x5165;&#x7B2C;&#x4E09;&#x65B9;&#x6269;&#x5C55;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>django-redis</strong>&#x6765;&#x89E3;&#x51B3;&#x3002;</p>
<p>1&#xFF09; &#x5B89;&#x88C5;&#x6269;&#x5C55;</p>
<pre><code class="lang-python">pip install django-redis
</code></pre>
<p>2&#xFF09;&#x914D;&#x7F6E;</p>
<p>&#x5728;settings.py&#x6587;&#x4EF6;&#x4E2D;&#x505A;&#x5982;&#x4E0B;&#x8BBE;&#x7F6E;</p>
<pre><code class="lang-python">CACHES = {
    <span class="hljs-string">&quot;default&quot;</span>: {
        <span class="hljs-string">&quot;BACKEND&quot;</span>: <span class="hljs-string">&quot;django_redis.cache.RedisCache&quot;</span>,
        <span class="hljs-string">&quot;LOCATION&quot;</span>: <span class="hljs-string">&quot;redis://127.0.0.1:6379/1&quot;</span>,
        <span class="hljs-string">&quot;OPTIONS&quot;</span>: {
            <span class="hljs-string">&quot;CLIENT_CLASS&quot;</span>: <span class="hljs-string">&quot;django_redis.client.DefaultClient&quot;</span>,
        }
    }
}
SESSION_ENGINE = <span class="hljs-string">&quot;django.contrib.sessions.backends.cache&quot;</span>
SESSION_CACHE_ALIAS = <span class="hljs-string">&quot;default&quot;</span>
</code></pre>
<h4 id="&#x6CE8;&#x610F;">&#x6CE8;&#x610F;</h4>
<p> &#x5982;&#x679C;redis&#x7684;ip&#x5730;&#x5740;&#x4E0D;&#x662F;&#x672C;&#x5730;&#x56DE;&#x73AF;127.0.0.1&#xFF0C;&#x800C;&#x662F;&#x5176;&#x4ED6;&#x5730;&#x5740;&#xFF0C;&#x8BBF;&#x95EE;Django&#x65F6;&#xFF0C;&#x53EF;&#x80FD;&#x51FA;&#x73B0;Redis&#x8FDE;&#x63A5;&#x9519;&#x8BEF;&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="../images/redis_connect_error.png" alt="redis&#x8FDE;&#x63A5;&#x9519;&#x8BEF;"></p>
<p>&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#xFF1A;</p>
<p>&#x4FEE;&#x6539;redis&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x6DFB;&#x52A0;&#x7279;&#x5B9A;ip&#x5730;&#x5740;&#x3002;</p>
<p>&#x6253;&#x5F00;redis&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</p>
<pre><code class="lang-shell">sudo vim /etc/redis/redis.conf
</code></pre>
<p>&#x5728;&#x5982;&#x4E0B;&#x914D;&#x7F6E;&#x9879;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF08;&#x5982;&#x8981;&#x6DFB;&#x52A0;10.211.55.5&#x5730;&#x5740;&#xFF09;</p>
<p><img src="../images/modify_redis_config.png" alt="&#x4FEE;&#x6539;redis&#x914D;&#x7F6E;&#x6587;&#x4EF6;"></p>
<p>&#x91CD;&#x65B0;&#x542F;&#x52A8;redis&#x670D;&#x52A1;</p>
<pre><code class="lang-shell">sudo service redis-server restart
</code></pre>
<h2 id="3-session&#x64CD;&#x4F5C;">3 Session&#x64CD;&#x4F5C;</h2>
<p>&#x901A;&#x8FC7;HttpRequest&#x5BF9;&#x8C61;&#x7684;session&#x5C5E;&#x6027;&#x8FDB;&#x884C;&#x4F1A;&#x8BDD;&#x7684;&#x8BFB;&#x5199;&#x64CD;&#x4F5C;&#x3002;</p>
<p>1&#xFF09; &#x4EE5;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x683C;&#x5F0F;&#x5199;session&#x3002;</p>
<pre><code>request.session[&apos;&#x952E;&apos;]=&#x503C;
</code></pre><p>2&#xFF09;&#x6839;&#x636E;&#x952E;&#x8BFB;&#x53D6;&#x503C;&#x3002;</p>
<pre><code>request.session.get(&apos;&#x952E;&apos;,&#x9ED8;&#x8BA4;&#x503C;)
</code></pre><p>3&#xFF09;&#x6E05;&#x9664;&#x6240;&#x6709;session&#xFF0C;&#x5728;&#x5B58;&#x50A8;&#x4E2D;&#x5220;&#x9664;&#x503C;&#x90E8;&#x5206;&#x3002;</p>
<pre><code>request.session.clear()
</code></pre><p>4&#xFF09;&#x6E05;&#x9664;session&#x6570;&#x636E;&#xFF0C;&#x5728;&#x5B58;&#x50A8;&#x4E2D;&#x5220;&#x9664;session&#x7684;&#x6574;&#x6761;&#x6570;&#x636E;&#x3002;</p>
<pre><code>request.session.flush()
</code></pre><p>5&#xFF09;&#x5220;&#x9664;session&#x4E2D;&#x7684;&#x6307;&#x5B9A;&#x952E;&#x53CA;&#x503C;&#xFF0C;&#x5728;&#x5B58;&#x50A8;&#x4E2D;&#x53EA;&#x5220;&#x9664;&#x67D0;&#x4E2A;&#x952E;&#x53CA;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x3002;</p>
<pre><code>del request.session[&apos;&#x952E;&apos;]
</code></pre><p>6&#xFF09;&#x8BBE;&#x7F6E;session&#x7684;&#x6709;&#x6548;&#x671F;</p>
<pre><code>request.session.set_expiry(value)
</code></pre><ul>
<li>&#x5982;&#x679C;value&#x662F;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#xFF0C;session&#x5C06;&#x5728;value&#x79D2;&#x6CA1;&#x6709;&#x6D3B;&#x52A8;&#x540E;&#x8FC7;&#x671F;&#x3002;</li>
<li>&#x5982;&#x679C;value&#x4E3A;0&#xFF0C;&#x90A3;&#x4E48;&#x7528;&#x6237;session&#x7684;Cookie&#x5C06;&#x5728;&#x7528;&#x6237;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x5173;&#x95ED;&#x65F6;&#x8FC7;&#x671F;&#x3002;</li>
<li>&#x5982;&#x679C;value&#x4E3A;None&#xFF0C;&#x90A3;&#x4E48;session&#x6709;&#x6548;&#x671F;&#x5C06;&#x91C7;&#x7528;&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;<strong>&#x9ED8;&#x8BA4;&#x4E3A;&#x4E24;&#x5468;</strong>&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5728;settings.py&#x4E2D;&#x8BBE;&#x7F6E;<strong>SESSION_COOKIE_AGE</strong>&#x6765;&#x8BBE;&#x7F6E;&#x5168;&#x5C40;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C03-RequestAndResponse/Cookie.html" class="navigation navigation-prev " aria-label="Previous page: Cookie"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C04-ClassViewAndMiddleware/index.html" class="navigation navigation-next " aria-label="Next page: 类视图与中间件"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book django项目 -->
</html>
